<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        position: relative;
        width: 400px;
        border-bottom: 1px solid red;
        margin: 100px auto;
      }

      .box input {
        width: 370px;
        height: 30px;
        border: 0;
        outline: none;
      }

      label {
        display: block;
        position: absolute;
        top: 50%;
        right: 20px;
        transform: translateY(-50%);

        width: 12px;
        height: 12px;
        border-radius: 50%;
        /* border: 1px solid skyblue; */
      }

      label img {
        display: block;
        width: 100%;
        height: 100%;
      }
    </style>
  </head>

  <body>
    <div class="box">
      <label for="">
        <img src="../img/close.png" alt="" id="eye" />
      </label>
      <input type="password" name="" id="pwd" />
    </div>

    <script>
      // 1. 获取元素
      var eye = document.getElementById('eye')
      let pwd = document.getElementById('pwd')

      var flag = 0
      eye.onclick = function () {
        //点击一次后，flag一定要变化
        if (flag == 0) {
          pwd.type = 'text'
          eye.src = '../img/open.png'
          flag = 1
        } else {
          pwd.type = 'password'
          eye.src = '../img/close.png'
          flag = 0
        }
      }
    </script>
  </body>
</html>
